﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Markup;

namespace Gronia.WPF.Controls
{
    public partial class StretchPanel : Panel
    {
        protected override Size ArrangeOverride(Size finalSize)
        {
            List<UIElement> children = new List<UIElement>();
            foreach (UIElement element in InternalChildren)
            {
                if (ChildrenOrder == ChildOrder.Reverse)
                {
                    children.Insert(0, element);
                    continue;
                }
                children.Add(element);
            }

            double finalWidth = double.NaN;
            double finalHeight = double.NaN;
            int childCount = children.Count(t => t != null);
            double lw = finalSize.Width - (childCount - 1) * Gap;
            double lh = finalSize.Height - (childCount - 1) * Gap;
            if (lw < 0 || lh < 0)
            {
                lw = 0;
                lh = 0;
            }

            Size finalS = new Size(lw, lh);
            if (Orientation == Orientation.Horizontal)
            {
                finalWidth = finalS.Width / childCount;
            }
            else if (Orientation == Orientation.Vertical)
            {
                finalHeight = finalS.Height / childCount;
            }
            double x = 0;
            double y = 0;
            for (int i = 0, count = children.Count; i < count; i++)
            {
                UIElement child = children[i];
                if (child == null)
                {
                    continue;
                }

                child.Arrange(new Rect(x, y, Orientation == Orientation.Horizontal ? finalWidth : child.DesiredSize.Width, Orientation == Orientation.Vertical ? finalHeight : child.DesiredSize.Height));

                x += Orientation == Orientation.Horizontal ? (finalWidth + (i == count ? 0 : Gap)) : 0;
                y += Orientation == Orientation.Vertical ? (finalHeight + (i == count ? 0 : Gap)) : 0;
            }
            return finalSize;
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            List<UIElement> children = new List<UIElement>();
            foreach (UIElement element in InternalChildren)
            {
                if (ChildrenOrder == ChildOrder.Reverse)
                {
                    children.Insert(0, element);
                    continue;
                }
                children.Add(element);
            }

            double finalWidth = double.NaN;
            double finalHeight = double.NaN;

            int childCount = children.Count(t => t != null);
            if (Orientation == Orientation.Horizontal)
            {
                finalWidth = availableSize.Width / childCount;
            }
            else if (Orientation == Orientation.Vertical)
            {
                finalHeight = availableSize.Height / childCount;
            }

            foreach (UIElement child in children)
            {
                if (child == null)
                {
                    continue;
                }

                child.Measure(new Size(Orientation == Orientation.Horizontal ? finalWidth : availableSize.Width, Orientation == Orientation.Vertical ? finalHeight : availableSize.Height));
            }
            return availableSize;
        }
    }
}